package com.fhd.servlet;

import com.fhd.constant.Constants;
import com.fhd.service.Client.ClientService;
import com.fhd.service.Client.ClientServiceImplement;
import com.fhd.service.Merchant.MerchantService;
import com.fhd.service.Merchant.MerchantServiceImplement;
import com.fhd.utils.DateTimeUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Objects;

public class SignUpServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String method = (String)req.getParameter("method");
            // 判断是那种注册
            if (Objects.equals(method,"signUpClient")) {
                signUpClient(req, resp);
            } else if (Objects.equals(method,"signUpMerchant")){
                signUpMerchant(req, resp);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    // 客户注册
    public void signUpClient(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userCode = req.getParameter("userCode");
        String userPassword = req.getParameter("userPassword");
        String userName = req.getParameter("userName");

        // 调用业务层
        ClientService clientService = new ClientServiceImplement();
        int row = clientService.signup(userCode, userPassword, userName);

        if (row > 0) {
            // 注册成功，回到登录页，返回信息
            req.getSession().setAttribute(Constants.SIGN_UP_MESSAGE,"注册成功，请登录");
            resp.sendRedirect("login.jsp");
        } else {
            // 注册失败，回到注册页，返回信息
            req.getSession().setAttribute(Constants.SIGN_UP_MESSAGE,"注册失败，请重试");
            resp.sendRedirect("signUpClient.jsp");
        }
    }

    // 商户注册
    public void signUpMerchant(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        String userCode = req.getParameter("userCode");
        String userPassword = req.getParameter("userPassword");
        String userName =  req.getParameter("userName");
        String shopName =  req.getParameter("shopName");
        String address =  req.getParameter("address");
        String phone =  req.getParameter("phone");
        String keyWord = req.getParameter("keyWord");
        // String 转成 Time
        Time workTimeBegin = DateTimeUtil.stringToTime(req.getParameter("workTimeBegin"));
        Time workTimeEnd = DateTimeUtil.stringToTime(req.getParameter("workTimeEnd"));


        // 调用业务层
        MerchantService merchantService = new MerchantServiceImplement();
        int row = merchantService.signup(userCode, userPassword, userName,shopName,address,phone,keyWord,workTimeBegin,workTimeEnd);

        if (row > 0) {
            // 注册成功，回到登录页，返回信息
            req.getSession().setAttribute(Constants.SIGN_UP_MESSAGE,"注册成功，请登录");
            resp.sendRedirect("login.jsp");
        } else {
            // 注册失败，回到注册页，返回信息
            req.getSession().setAttribute(Constants.SIGN_UP_MESSAGE,"注册失败，请重试");
            resp.sendRedirect("signUpMerchant.jsp");
        }
    }
}
